#file:c:\Users\Zhou\Desktop\training-mall-management-end\src\components\admin\AdvertisementsView.vue
<template>
    <div class="advertisements-view">
        <h2>广告管理</h2>
        <div class="ad-list">
            <div v-for="ad in advertisements" :key="ad.productid" class="ad-item" @click="viewProductDetail(ad.productid)">
                <img :src="ad.adimg || '/default-ad.png'" alt="Ad Image" class="ad-image" @error="handleImageError" />
                <div class="ad-details">
                    <h3>{{ ad.title }}</h3>
                    <p>{{ ad.description }}</p>
                </div>
                <el-button class="edit-btn" type="primary" @click.stop="editProduct(ad.productid)">编辑</el-button>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { getAdProduct } from '../../api/adver';

const router = useRouter();
const advertisements = ref([]);

// 图片加载失败处理
const handleImageError = (e) => {
    //e.target.src = '/default-ad.png'; // 使用默认广告图片
    //console.error('广告图片加载失败:');
};

// 获取广告产品数据
const fetchAdProducts = async () => {
    try {
        const response = await getAdProduct();
        if (response.data.code === 1) {
            advertisements.value = response.data.data.map(product => ({
                productid: product.productid,
                title: product.title,
                description: product.description,
                status: product.onsale ? '启用' : '停用',
                adimg: product.adimg || '/default-ad.png', // 确保 adimg 存在
                userid: product.userid || '' // 确保 userid 存在
            }));
        } else {
            console.error('获取广告产品列表失败:', response.msg);
        }
    } catch (err) {
        console.error('请求捕获异常:', err.message, '完整错误:', err);
    }
};

// 查看商品详情
const viewProductDetail = (productid) => {
    router.push({ name: 'productDetailById', params: { productid } });
};

// 编辑广告商品
const editProduct = (productid) => {
    router.push({ name: 'productDetailById', params: { productid } });
};

onMounted(() => {
    fetchAdProducts();
});
</script>

<style scoped>
.advertisements-view {
    padding: var(--padding-base);
    background-color: var(--background-color);
}

.ad-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 1rem;
}

.ad-item {
    background-color: #f9fafb;
    border: 1px solid #ebebeb;
    border-radius: var(--border-radius);
    padding: 1rem;
    display: flex;
    align-items: center;
    transition: box-shadow 0.3s ease;
    cursor: pointer;
    /* 添加鼠标指针样式 */
}

.ad-item:hover {
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.ad-image {
    max-width: 100px;
    height: auto;
    border-radius: var(--border-radius);
    margin-right: 1rem;
}

.ad-details {
    flex: 1;
}

.ad-status {
    color: var(--primary-color);
    font-weight: bold;
}

.edit-btn {
    background-color: var(--primary-color);
    color: white;
    border: none;
    border-radius: var(--border-radius);
    padding: 0.5rem 1rem;
    cursor: pointer;
    transition: background-color 0.3s ease;
    margin-left: 0.5rem;
}

.edit-btn:hover {
    background-color: darken(var(--primary-color), 10%);
}
</style>